package com.example.mypplication.sortlist.newsortlist


import android.content.Context
import android.graphics.*
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
class SideBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : View(context, attrs, defStyle) {

    interface OnTouchingLetterChangedListener {
        fun onTouchingLetterChanged(s: String)
    }

    private var onTouchingLetterChangedListener: OnTouchingLetterChangedListener? = null
    private var choose = -1
    private val paint = Paint()
    private var mTextDialog: TextView? = null

    // 所有可能的字母（包括#）
    private val allLetters = arrayOf("A", "B", "C", "D", "E", "F", "G", "H", "I",
        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#")

    // 实际显示的字母
    private var displayedLetters = emptyArray<String>()

    fun setDisplayLetters(letters: Array<String>) {
        displayedLetters = letters
        invalidate() // 重绘视图
        requestLayout() // 请求重新布局
    }

    fun setTextView(textView: TextView) {
        this.mTextDialog = textView
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 根据显示的字母数量计算高度
        val height = if (displayedLetters.isNotEmpty()) {
            (displayedLetters.size * 40).coerceAtLeast(100) // 每个字母40px，最小高度100px
        } else {
            0
        }
        setMeasuredDimension(widthMeasureSpec, height)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (displayedLetters.isEmpty()) return

        val height = height
        val width = width
        val singleHeight = height / displayedLetters.size

        for (i in displayedLetters.indices) {
            val letter = displayedLetters[i]
            paint.color = Color.BLUE
            paint.isAntiAlias = true
            paint.textSize = 14f

            if (i == choose) {
                paint.color = Color.parseColor("#B8B8B8")
                paint.isFakeBoldText = true
            }

            val xPos = width / 2 - paint.measureText(letter) / 2
            val yPos = singleHeight * i + singleHeight
            canvas.drawText(letter, xPos, yPos.toFloat(), paint)
            paint.reset()
        }
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        if (displayedLetters.isEmpty()) return true

        val action = event.action
        val y = event.y
        val oldChoose = choose
        val listener = onTouchingLetterChangedListener
        val c = (y / height * displayedLetters.size).toInt()

        when (action) {
            MotionEvent.ACTION_UP -> {
                background = ColorDrawable(0x00000000)
                choose = -1
                invalidate()
                mTextDialog?.visibility = View.INVISIBLE
            }
            else -> {
                if (oldChoose != c && c in displayedLetters.indices) {
                    val letter = displayedLetters[c]
                    listener?.onTouchingLetterChanged(letter)
                    mTextDialog?.apply {
                        text = letter
                        visibility = View.VISIBLE
                    }
                    choose = c
                    invalidate()
                }
            }
        }
        return true
    }

    fun setOnTouchingLetterChangedListener(listener: OnTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = listener
    }
}